package com.pqc.tbk.web.money;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.pqc.tbk.entity.ConstProp;
import com.pqc.tbk.entity.money.MoneyInout;
import com.pqc.tbk.service.money.MoneyInoutService;
import com.pqc.tbk.utils.Pager;
import com.pqc.tbk.utils.UserUtil;

/**
 * 
 * @author long, create on 2012-8-8 下午1:55:18
 * 
 */
@Controller
@RequestMapping("/user/home")
public class MoneyInoutController {

	private MoneyInoutService moneyInoutService;

	@RequestMapping("/money_out_history")
	public String money_out_histroy() {
		return "/money/money_out_history";
	}

	@RequestMapping("/money_promote_history")
	public String money_in_histroy() {
		return "/money/money_promote_history";
	}

	/**
	 * 订单列表进行分页的结果
	 * 
	 * @param cpage
	 * @param map
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "outList/{cpage}")
	public String getOutPages(@PathVariable("cpage") final long cpage,
			final ModelMap map) {
		final long uid = UserUtil.getUID();
		Pager<MoneyInout> pager = initPager(cpage, ConstProp.MoneyInout.OUT,
				uid);
		pager = moneyInoutService
				.getPages(pager, ConstProp.MoneyInout.OUT, uid);
		map.put("pager", pager);
		return "money/money_out_history_table";
	}

	/**
	 * 订单列表进行分页的结果
	 * 
	 * @param cpage
	 * @param map
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "promoteList/{cpage}")
	public String getPromotePages(@PathVariable("cpage") final long cpage,
			final ModelMap map) {
		final long uid = UserUtil.getUID();
		Pager<MoneyInout> pager = initPager(cpage,
				ConstProp.MoneyInout.PROMOTE, uid);
		pager = moneyInoutService.getPages(pager, ConstProp.MoneyInout.PROMOTE,
				uid);
		map.put("pager", pager);
		return "money/money_promote_history_table";
	}

	// pager对象的初始化
	public Pager<MoneyInout> initPager(long cpage, final String type,
			final long uid) {
		final Pager<MoneyInout> pager = new Pager<MoneyInout>();
		// 到时候pagesize从Application中获取
		pager.setPageSize(10L);
		pager.setTotal(moneyInoutService.getCountByUid(type, uid));
		final long pages = (pager.getTotal() % pager.getPageSize()) == 0 ? pager
				.getTotal() / pager.getPageSize()
				: (pager.getTotal() / pager.getPageSize()) + 1;
		pager.setPages(pages);
		if (cpage > pager.getPages()) {
			cpage = pager.getPages();
		}
		if (cpage < 1) {
			cpage = 1L;
		}
		pager.setNum(cpage);
		pager.setOffSet((cpage - 1) * pager.getPageSize());
		return pager;
	}

	@Autowired
	public void setMoneyInoutService(final MoneyInoutService moneyInoutService) {
		this.moneyInoutService = moneyInoutService;
	}

}
